package com.hb.game.pk.ui.activity.contact;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.FriendBean;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.event.MoneyChangeEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.numbercodeview.BaseNumberCodeView;
import com.hb.game.pk.widgets.numbercodeview.bottomsheet.BottomSheetNumberCodeView;
import com.hb.game.pk.widgets.titlebar.TitleBar;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TransActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.btn_trans)
    Button btnTrans;
    FriendBean.DataBean friendData;
    @BindView(R.id.bsn_pwd)
    BottomSheetNumberCodeView bsnPwd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_trans;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        String friendStr = intent.getStringExtra("friend");
        friendData = new Gson().fromJson(friendStr, FriendBean.DataBean.class);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransActivity.this.finish();
            }
        });

        String trim = StringUtils.getString(friendData.getFace());
        int res = 0;
        if (StringUtils.isEmpty(trim)) {
            res = R.mipmap.ic_head_normal;
        } else {
            int i = Integer.parseInt(trim);
            res = APP.headResource.get(i - 1);
        }
        ivHead.setBackgroundResource(res);
        tvName.setText(friendData.getNickname());
        bsnPwd.setIsPassword(true);
    }

    @OnClick(R.id.btn_trans)
    public void onViewClicked() {
        String money = etMoney.getText().toString();
        if (StringUtils.isEmpty(money)) {
            ToastUtils.showToast("请输入转账金额");
            return;
        }
//        BottomSheetNumberCodeViewActivity.show(this, true);
        bsnPwd.setVisibility(View.VISIBLE);
        bsnPwd.showNumberCodeLayout();
        bsnPwd.setOnHideBottomLayoutListener(new BottomSheetNumberCodeView.OnHideBottomLayoutListener() {
            @Override
            public void onHide() {
                bsnPwd.setVisibility(View.GONE);
                hiddenKeyboard();
            }
        });
        bsnPwd.setNumberCodeCallback(new BaseNumberCodeView.OnInputNumberCodeCallback() {
            @Override
            public void onResult(String code) {
                if (StringUtils.isEmpty(code))
                    return;
                call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).transfer(friendData.getUser_id(), etMoney.getText().toString(), Integer.parseInt(code));
                call.enqueue(new Callback<NullBean>() {
                    @Override
                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                        NullBean body = response.body();
                        if (body == null) {
                            ToastUtils.showToast("请求错误");
                            return;
                        }

                        if (body.getStatus() != 1) {
                            ToastUtils.showToast(body.getInfo());
                            return;
                        }
                        ToastUtils.showToast(body.getInfo());
                        EventBus.getDefault().post(new MoneyChangeEvent());
                        TransActivity.this.finish();
                    }

                    @Override
                    public void onFailure(Call<NullBean> call, Throwable t) {
                        ToastUtils.showToast("网络请求错误");
                    }
                });
            }
        });
    }
}
